﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace wuliuback
{
    public class DbHelper
    { 
        private string strError = null;
        private int intCount = 0;
        private static SqlConnection conn = null;
        public DbHelper()
        {
            if (conn == null)
            {
                conn = DBconn();
            }
        }          
        /// < summary>          /// 公开方法DBConn，返回数据库连接          /// < /summary>      
        /// /// < returns>< /returns>          
        /// 
        public static SqlConnection DBconn()         
        {
            string strConn = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
            
            try             
            {                  
                return new SqlConnection(strConn);              
            }              
            catch (Exception)               
            {                 
                return null;      
            }   
        }         
        /// < summary>          
        /// 公开属性ErrorMessage，返回错误信息          
        /// < /summary>          
        public string ErrorMessage          
        {        
            get    
            {         
                return strError;    
            }       
        }      
        /// < summary>     
        /// 根据查询语句从数据库检索数据    
        /// < /summary>         
        /// < param name="strSelect">查询语句< /param>            
        /// < param name="SqlConn">数据库连接< /param>          
        /// < returns>有数据则返回DataSet对象，否则返回null< /returns>         
        public DataSet Select(string SelectString)
        {        
            strError = "";        
            try          
            {           
                //若数据库连接的当前状态是关闭的，则打开连接             
                if (conn.State == ConnectionState.Closed)           
                {                  
                    conn.Open();      
                }                 
                SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();  
                SqlCommand selectCommand = new SqlCommand(SelectString, conn);       
                selectCommand.CommandType = CommandType.Text;    
                mySqlDataAdapter.SelectCommand = selectCommand;    
                DataSet myDS = new DataSet();           
                mySqlDataAdapter.Fill(myDS);    
                return myDS;       
            }         
            catch (Exception e)    
            {               
                strError = "数据检索失败：" + e.Message;      
                return null;          
            }             
            finally      
            {            
                if (conn.State != ConnectionState.Closed)        
                {      
                    conn.Close();     
                }      
            }      
        }       
        /// < summary>          ///
        /// 更新数据库          ///
        /// < /summary>          ///
        /// < param name="UpdateString">Update Sql语句< /param>          ///
        /// < param name="SqlConn">数据库连接< /param>          /// 
        /// < returns>更新成功返回true< /returns>          
        public bool Update(string UpdateString) 
        {              
            return udiDataBase(UpdateString);    
        }          
        
        /// < summary>          ///
        /// 从数据库中删除数据          /// 
        /// < /summary>          /// 
        /// < param name="DeleteString">Delete Sql语句< /param>          ///
        /// < param name="SqlConn">数据库连接< /param>          ///
        /// < returns>删除成功返回true< /returns>          
        public bool Delete(string DeleteString)       
        {            
            return udiDataBase(DeleteString);     
        }          
        /// < summary>          ///
        /// 把数据插入数据库          /// 
        /// < /summary>          /// 
        /// < param name="InsertString">Insert Sql语句< /param>          /// 
        /// < param name="SqlConn">数据库连接< /param>          ///
        /// < returns>插入成功返回true< /returns>         
        public bool Insert(string InsertString)      
        {      
            return udiDataBase(InsertString);    
        }          
        /// < summary>          /// 
        /// 根据Sql语句更新数据库          ///
        /// < /summary>           ///
        /// < param name="UDIString">更新语句< /param>          /// 
        /// < param name="SqlConn">数据库连接< /param>          /// 
        /// < returns>更新成功则返回true< /returns>         
        public bool udiDataBase(string UDIString)        
        {          
            strError = "";               
            try        
            {            
                if (conn.State == ConnectionState.Closed)     
                {          
                    conn.Open();
                }                
                SqlCommand cmd = new SqlCommand(UDIString, conn);    
                cmd.CommandType = CommandType.Text;    
                intCount = cmd.ExecuteNonQuery();     
                return !(intCount <  1);        
            }            
            catch (Exception e)    
            {              
                strError = "更新数据库失败：" + e.Message;          
                return false;      
            }          
            finally   
            {            
                if (conn.State != ConnectionState.Closed)     
                {                  
                    conn.Close();        
                }           
            }        
        }    
    } 
}  